<script setup>
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
// ------------------------------用户搜索
const formInline = reactive({
    userId: '',
    username: '',
    nickname: '',
    regst: 'all',
    reg: 'all',
    hot: 'all',
    dis: 'all',
    sup: 'all',
    dic: 'all',
    regTime: ''
})

const onSubmit = () => {
    console.log('submit!')
}
// ------------------------------用户数据
let users = reactive([
    {
        sort: 1, id: "2001", name: "摇滚音乐", avatar: "../public/ygyy.png", addTime:"2024-08-25 12:27:56", 
        updateTime:"2024-08-25 12:28:32", del: 0
    },
    {
        sort: 2, id: "2002", name: "流行音乐", avatar: "../public/lxyy.png", addTime:"2024-08-25 10:27:56", 
        updateTime:"2024-08-25 12:54:32", del: 1
    },
    {
        sort: 3, id: "2003", name: "民谣音乐", avatar: "../public/myyy.png", addTime:"2024-08-25 18:27:56", 
        updateTime:"2024-08-27 17:28:52", del: 0
    },
    {
        sort: 4, id: "2004", name: "电子音乐", avatar: "../public/dzyy.png", addTime:"2024-08-26 10:47:34", 
        updateTime:"2024-08-29 19:45:42", del: 1
    },
    {
        sort: 5, id: "2005", name: "爵士音乐", avatar: "../public/jsyy.png", addTime:"2024-08-27 12:27:56", 
        updateTime:"2024-08-30 20:48:32", del: 0
    },
    {
        sort: 6, id: "2006", name: "原声器乐", avatar: "../public/ysqy.png", addTime:"2024-08-28 2:27:49", 
        updateTime:"2024-09-2 9:34:32", del: 0
    },
    
])

// ------------------------ 分页

const currentPage = ref(1)
const pageSize = ref(100)
const size = ref('default')
const background = ref(false)
const disabled = ref(false)

const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
}

// ---------------------- 删除用户操作
// 弹出二次确认对话框：参数user就是要删除的用户：
function showConfirm(user) {
    ElMessageBox.confirm(
    '确定要删除 ' + user.name +' 音乐分类吗?',
    '警告信息',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      // 调用删除函数：删除用户
      deleteUser(user)
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '管理员取消了删除操作',
      })
    })
    
}

// 删除用户的函数：参数user就是要删除的用户
function deleteUser(user) {
    // 查询用户在数组中的索引
    let index = users.findIndex(item => item.id === user.id)
    // 删除用户数据
    users.splice(index, 1)
    // 提示删除成功
    ElMessage({
        type: 'success',
        message: '管理员成功删除了用户 ' + user.name,
      })
}

// // ------------------ 新增用户操作 
// import DialogUserCreate from '../components/DialogUserCreate.vue'
// // 声明变量控制弹窗显示/隐藏
// let userDialogCreate = ref(false)
// // 准备函数：显示对话框
// function showCreateDialog() {
//     userDialogCreate.value = true
// }
// // 准备函数：隐藏对话框
// function hideCreateDialog() {
//     userDialogCreate.value = false
// }
// // 新增用户数据：参数user就是子组件告诉父组件要新增的用户
// function createUser(user) {
//     // 计算新用户的id
//     let id = users[0].id + 1
//     // 复制一份用户数据
//     let newUser = {...user}
//     // 给新数据添加id属性
//     newUser.id = id
//     // 新增用户
//     users.unshift(newUser)
// }


</script>
<template>
    <div class="p-header">
        <h2>音乐分类管理</h2>
        <el-button type="primary" @click="showCreateDialog">新增音乐分类</el-button>
    </div>
    <!-- 新增用户弹窗 -->
    <DialogUserCreate :dialogFormVisible="userDialogCreate"
        @createUserEvent="createUser"
        @closeCreateEvent="hideCreateDialog"></DialogUserCreate>

    <el-divider>音乐分类信息搜索</el-divider>
    <el-card>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            
            <el-form-item label="音乐分类名称：">
                <el-input v-model="formInline.nickname" placeholder="请输入音乐分类名称" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
            </el-form-item>
        </el-form>
    </el-card>
    <el-divider>音乐分类信息展示</el-divider>
    <el-table :data="users" style="width: 100%">
        <el-table-column label="序号" prop="sort" width="80"/>
        <el-table-column label="id" prop="id" width="180"/>
        <el-table-column label="分类名称" prop="name" width="150"/>
        <el-table-column label="分类图标" prop="avator" width="100">
            <template #default="{ row }">
                <el-avatar :src="row.avatar" :size="35"/>
            </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="addTime" width="220"/>
        <el-table-column label="修改时间时间" prop="updateTime" width="220"/>
        <el-table-column label="是否删除" prop="del" width="220">
            <template #default="{ row }">
                <el-tag type="primary" v-if="row.del==0">否</el-tag>
                <el-tag type="danger" v-else-if="row.del==1">是</el-tag>
            </template>
        </el-table-column>
        <el-table-column align="right" fixed="right" width="220">
            <template #default="scope">
                <el-button-group>
                    <el-button type="success" size="small">编辑</el-button>
                    <el-button type="danger" size="small" @click="showConfirm(scope.row)">删除</el-button>
                </el-button-group>
            </template>
        </el-table-column>
    </el-table>

    <el-pagination
      background
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[100, 200, 300, 400]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="1000"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
</template>
<style scoped>
.demo-form-inline .el-input {
    --el-input-width: 220px;
}

.demo-form-inline .el-select {
    --el-select-width: 100px;
    font-size: 14px;
}

.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}

.el-pagination{
    margin-top:20px;
}

.p-header{
    display:flex;
    justify-content: space-between;
}
</style>